import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		store: {},
		cart: [],
		orderType: 'takein',
		address: {},
		addresses: '',
		member: uni.getStorageSync('userInfo') || {},
		order: {},
		cartGoods: [],
		refresh:false,
	},

	mutations: {
		SET_STATUS(state,val){
			state.refresh = val
		},
		SET_GOODS(state, val) {
			if (state.cartGoods.length) {
				let res =  state.cartGoods.findIndex((item, index) => {
					return item.category_id === val.category_id && item.id === val.id && JSON.stringify(
							item.cups.id) === JSON.stringify(val.cups.id) && JSON.stringify(item
							.attribute) === JSON.stringify(val.attribute) && JSON.stringify(item
							.toppings) === JSON.stringify(val.toppings)
				})
				if(res == -1){
					state.cartGoods.push(val)
				}else{
					state.cartGoods[res].num += val.num
				}
				console.log(res,9999);
			} else {
				state.cartGoods.push(val)
			}
		},
		clear_cart(state){
			console.log(1234);
			state.cartGoods = []
		},
		add_cartGoods(state,index){
			state.cartGoods[index].num++
			// 重新计算商品价格
			console.log(state.cartGoods[index],90);
			// let price = 0
			// if(state.cartGoods[index].toppings.length>0){
			// 	state.cartGoods[index].toppings.forEach(item=>{
			// 		price += Number(item.price)*Number(item.num)
			// 	})
			// }
			// state.cartGoods[index].totalPrice = (state.cartGoods[index].cups.price + price)*state.cartGoods[index].num
		},
		reduce_cartGoods(state,index){
			state.cartGoods[index].num--
			// console.log(state.cartGoods[index].num,99999);
			// 重新计算商品价格
			// // console.log(state.cartGoods[index],90);
			// let price = 0
			// if(state.cartGoods[index].toppings.length>0){
			// 	state.cartGoods[index].toppings.forEach(item=>{
			// 		price += Number(item.price)*Number(item.num)
			// 	})
			// }
			// state.cartGoods[index].totalPrice = (state.cartGoods[index].cups.price + price)*state.cartGoods[index].num
			if(!state.cartGoods[index].num){
				  state.cartGoods.splice(index,1)
			}
		},
		SET_ORDER_TYPE(state, type) {
			state.orderType = type
		},
		SET_MEMBER(state, member) {
			state.member = member
		},
		SET_ADDRESS(state, address) {
			state.address = address
		},
		SET_ADDRESSES(state, addresses) {
			state.addresses = addresses
		},
		SET_STORE(state, store) {
			console.log(store,99999);
			state.store = store
		},
		SET_CART(state, cart) {
			state.cart = cart
		},
		REMOVE_CART(state) {
			state.cart = []
		},
		SET_ORDER(state, order) {
			state.order = order
		}
	},
	actions: {

	},
	getters: {
		isLogin: state => Object.keys(state.member).length > 0 ,//是否登录
		//计算商品价格
		totalPrice(state) {
			let data = {
				zn: 0,
				zp: 0
			}
			let price = state.cartGoods.reduce((acc, cur) => acc + cur.totalPrice*cur.num, 0)
			let allNmu =  state.cartGoods.reduce((acc, cur) => acc + cur.num, 0)
			console.log(allNmu,9090909);
			data.zp = (price/100).toFixed(2)
			data.zn = allNmu
			return data
		},
	}
})

export default store